package com.jijs.guava.base;

import com.google.common.base.Strings;

/**
 * Created by jjs on 2016/12/31.
 * @author jijs
 */
public class StringsTest {

    /**
     * 1．Strings.padEnd方法
     * <p>
     * padEnd补全右
     */
    public static void padEnd() {
        String a = "12345";
        String b = Strings.padEnd(a, 10, 'x');
        System.out.println(b);  //结果为：12345xxxxx
    }

    public static void padStart() {
        String a = "12345";
        String b = Strings.padStart(a, 10, 'x');
        System.out.println(b);  //结果为：xxxxx12345
    }

    /**
     * 3．Strings.isNullOrEmpty 方法
     * <p>
     * Strings.isNullOrEmpty 校验空值
     */
    public static void isNullOrEmpty() {
        String a = "";
        boolean b = Strings.isNullOrEmpty(a);
        System.out.println(b);  // 结果为：true

    }

    /**
     * Strings.nullToEmpty  方法
     * Strings.nullToEmpty  如果为null 转为""
     */
    public static void nullToEmpty() {
        String a = null;
        String b = Strings.nullToEmpty(a);
        System.out.println(b);//结果为：
    }

    /**
     * 5．Strings.emptyToNull  方法
     * Strings.emptyToNull  如果为"" 转为null
     */
    public static void emptyToNull() {
        String a = "";
        String b = Strings.emptyToNull(a);
        System.out.println(b);  //结果为：null
    }

    /**
     * 6．Strings.repeat  方法
     * Strings.repeat  重复字符串
     */
    public static void repeat() {
        String a = "123";
        String b = Strings.repeat(a, 3);
        System.out.println(b);  //结果为：123123123
    }

    /**
     * 7．Strings.commonPrefix 方法
     * Strings.commonPrefix 获取a,b左公共部分字符串
     */
    public static void commonPrefix() {
        String a = "abcdsfsfs";
        String b = "abc3sfsd";
        String c = Strings.commonPrefix(a, b);
        System.out.println(c);  //结果为：abc
    }

    /**
     * 8．Strings.commonSuffix 方法
     * Strings.commonSuffix 获取a,b右公共部分字符串
     */
    public static void commonSuffix() {
        String a = "faaxyz";
        String b = "fwefxyz";
        String c = Strings.commonSuffix(a, b);
        System.out.println(c);
        System.out.println(c);  ////结果为：xyz
    }

    public static void main(String[] args) {
        padEnd();
        padStart();
        isNullOrEmpty();
        nullToEmpty();
        emptyToNull();
        repeat();
        commonPrefix();
        commonSuffix();
    }
}
